package com.gateway.security.common.authentication.client;

import com.gateway.common.dto.ApiDefinition;
import com.gateway.common.enums.AuthenticationTypeEnum;
import com.gateway.security.common.token.SecurityToken;
import org.springframework.security.authentication.AbstractAuthenticationToken;

import java.io.Serial;
import java.util.Collections;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/10 23:07
 **/
public class ClientAuthenticationToken extends AbstractAuthenticationToken {

    @Serial
    private static final long serialVersionUID = -3340879891318950683L;

    private ClientPrincipal principal;

    private final SecurityToken token;

    private final ApiDefinition apiDefinition;

    public ClientAuthenticationToken(
            ClientPrincipal principal,
            SecurityToken token,
            ApiDefinition apiDefinition) {
        super(principal.getAuthorities());
        this.principal = principal;
        this.token = token;
        this.apiDefinition = apiDefinition;
        this.setAuthenticated(true);
    }


    public ClientAuthenticationToken(
            Boolean skip,
            SecurityToken token,
            ApiDefinition apiDefinition) {
        super(Collections.emptyList());
        this.token = token;
        this.apiDefinition = apiDefinition;
    }

    public ClientAuthenticationToken(SecurityToken clientToken, ApiDefinition apiDefinition) {
        super(Collections.emptyList());
        this.token = clientToken;
        this.apiDefinition = apiDefinition;
    }


    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public ClientPrincipal getPrincipal() {
        return principal;
    }

    public SecurityToken getToken() {
        return token;
    }

    public ApiDefinition getApiDefinition() {
        return apiDefinition;
    }

    @Override
    public String toString() {
        return getToken().getToken();
    }
}
